package com.tanhua.dubbo.server.api;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.sun.org.apache.regexp.internal.RE;
import com.tanhua.dubbo.server.enums.AccountrState;
import com.tanhua.dubbo.server.enums.FreezingRangeEnum;
import com.tanhua.dubbo.server.enums.FreezingTimeEnum;
import com.tanhua.dubbo.server.pojo.Frozen;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;

@Service(version = "1.0.0")
@Slf4j
public class ManageFreezeApiImpl implements ManageFreezeApi {

    //调用mongodb
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 用户冻结
     *
     * @param
     * @return
     */
    @Override
    public Boolean manageFreeze(Integer userId, Integer freezingTime, Integer freezingRange, String reasonForFreezing, String forzenRemarks, AccountrState accountrState) {
        //创建表对象
        Frozen frozen = new Frozen();
        frozen.setId(ObjectId.get());
        frozen.setUserId(Convert.toLong(userId));
        frozen.setFreezingTime(freezingTime);
        frozen.setFreezingRange(freezingRange);
        frozen.setReasonsForFreezing(reasonForFreezing);
        frozen.setFrozenRemarks(forzenRemarks);
        frozen.setCreated(System.currentTimeMillis());
        frozen.setFrozenState(accountrState.getValue());
        //向冻结表中填充数据
        Frozen save = this.mongoTemplate.save(frozen);
        if (ObjectUtil.isEmpty(save)) {
            return false;
        }
        return true;
    }

    /**
     * 解冻用户
     *
     * @param userId
     * @param reasonsForThawing
     * @param accountrState
     * @return
     */
    @Override
    public Boolean manageUnfreeze(Integer userId, String reasonsForThawing, AccountrState accountrState) {
        Boolean result1 = this.isAccountState(userId, FreezingRangeEnum.DYNAMIC);
        Boolean result2 = this.isAccountState(userId, FreezingRangeEnum.MAKE);
        Boolean result3 = this.isAccountState(userId, FreezingRangeEnum.REGISTER);
        if ((!result1) && (!result2) && (!result3)) {
            return true;
        }
        //创建表对象
        Frozen frozen = new Frozen();
        //向冻结表中填充数据
        frozen.setCreated(System.currentTimeMillis());
        frozen.setCause(reasonsForThawing);
        frozen.setUserId(Convert.toLong(userId));
        frozen.setFrozenState(accountrState.getValue());
        Frozen save = mongoTemplate.save(frozen);
        if (ObjectUtil.isEmpty(save)) {
            return false;
        } else {
            return true;
        }
    }



    /**
     * 判断当前用户的账号状态
     *
     * @param userId
     * @return
     */
    @Override
    public Boolean isAccountState(Integer userId, FreezingRangeEnum freezingRangeEnum) {
        //获取
        Query query = Query.query(Criteria.where("userId").is(Convert.toLong(userId))).with(Sort.by(Sort.Order.desc("created"))).limit(1);

        //查找最近的冻结状态
        Frozen frozen = this.mongoTemplate.findOne(query, Frozen.class);

        if (ObjectUtil.isEmpty(frozen)) {
            //没有冻结状态返回未冻结
            return false;
        }
        //冻结状态
        //判断冻结类型为登录
        if (frozen.getFrozenState() == AccountrState.FREEZE.getValue() && freezingRangeEnum.getValue() == frozen.getFreezingRange()) {
            //获取创建冻结状态的时间
            Long created = frozen.getCreated();
            //获取现在的时间
            Date dateNow = new Date();
            Long nowTime = dateNow.getTime();
            //判断是冻结3天,7天,还是永久冻结
            if (frozen.getFreezingTime() == FreezingTimeEnum.THREE.getValue()) {
                if (nowTime >= (created + 1000 * 60 * 60 * 24 * 3)) {
                    //冻结时间已经过了
                    return false;
                } else {
                    //还在冻结
                    return true;
                }
            } else if (frozen.getFreezingTime() == FreezingTimeEnum.SEVEN.getValue()) {
                if (nowTime >= (created + 1000 * 60 * 60 * 24 * 7)) {
                    //冻结时间已经过了
                    return false;
                } else {
                    //还在冻结
                    return true;
                }
            } else {
                return true;
            }
        }
        //解冻状态
        return false;
    }


}


